use std::sync::Arc;
use dashmap::DashMap;
use crate::{CmdStrategy, Redis, RespFrame, Session, SimpleString};

pub struct SetCmd;


impl CmdStrategy for SetCmd {
    fn execute(&self, fragments: Vec<&str>, db: Redis, sesseion_id: String, sessions: Arc<DashMap<String, Session>>) -> Result<RespFrame, String> {
        let key = fragments[4];

        let value = fragments[6];

        let s = sessions.get(&sesseion_id);

        let db_index = s.unwrap().db_index;

        // println!("db_index {}", db_index);

        db.databases[db_index].write().unwrap().insert(key.to_string(), value.to_string());

        let str = SimpleString::new("Ok");

        let resp = RespFrame::SimpleString(str);
        Ok(resp)
    }
}